<template>
  <el-scrollbar>
    <div
      style="
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        background-color: #f3f5f9;
      "
    >
      <div v-for="(item, index) in userShopList" :key="index" class="box-all">
        <div class="logo">
          <img :src="'http://zxc.ruanjian211.top/' + item.logo" alt="" />
        </div>
        <div class="shopname">
          <span>店铺名:{{ item.name }}</span>
        </div>
        <div class="bottom">
          <div class="left" @click="goRegister(item.id)">修改</div>
          <div class="right" @click="goShop(item.id)">进入店铺</div>
        </div>
      </div>
    </div>
  </el-scrollbar>
</template>

<script setup lang="ts">
import {
  ref,
  reactive,
  toRefs,
  watch,
  onMounted,
  getCurrentInstance,
} from 'vue'
import { UserShopType } from '@/api/register/types/register'
import { getUserShop } from '@/api/register/register'
import { queryReq } from '@/utils/http/code'
import router from '@/router'
import useSellerRegisterStore from '@/store/modules/register'
import { shopLogin } from '@/api/login/login'
import { setToken, clearToken } from '@/utils/common/auth'
import { CodeEnum } from '@/constants/http-code'
import { ElMessage } from 'element-plus'

const { storeId } = toRefs(useSellerRegisterStore())

onMounted(() => {
  initShopList()
})
// 获取商店列表
const initShopList = async () => {
  const res = await getUserShop()
  queryReq(res, (data) => {
    userShopList.value = data
  })
}
const goRegister = (storeId: string | number) => {
  useSellerRegisterStore().setStoreId(storeId)
  router.push({ path: '/cvmagic/register/store', query: { storeId: storeId } })
}
const goShop = async (storeId: string | number) => {
  const res = await shopLogin(storeId)
  if (res.code === CodeEnum.OK) {
    setToken(res.data.accessToken, '')
    router.push('/')
  } else {
    ElMessage.error(res.msg)
  }
}

const userShopList = ref([] as Array<UserShopType>)
</script>
<style lang="scss" scoped>
.box-all {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: #fff;
  border-radius: 5px;
  margin: 10px 10px;
  .logo {
    width: 80px;
    height: 80px;
    margin: auto;
    padding-top: 20px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .shopname {
    padding: 0 10px;
    margin-top: 30px;
    font-size: 14px;
  }
  .bottom {
    width: 100%;
    position: absolute;
    bottom: 0;
    padding: 0 10px;
    cursor: pointer;
    display: flex;
    justify-content: space-around;
    .left {
      font-size: 12px;
      padding: 5px 0;
      color: #7b73ff;
    }
    .right {
      font-size: 12px;
      padding: 5px 0;
      color: #7b73ff;
    }
  }
}
</style>
